package cn.itcast.n3;

import cn.itcast.Constants;
import cn.itcast.n2.util.FileReader;
import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.TestState")
public class TestStates {
    private static Object lock = new Object();
    public static void main(String[] args) {
        final Thread t1 = new Thread(() -> {
            //未启动，NEW
            log.debug("running");
        }, "t1");

        final Thread t2 = new Thread(() -> {
            //RUNNABLE
            while (true){
                //runnable
            }
        }, "t2");
        t2.start();

        final Thread t3 = new Thread(() -> {
            //TERMINATED
            log.debug("running...");
        }, "t3");
        t3.start();

        final Thread t4 = new Thread(() -> {
            synchronized (lock){
                try {
                    //释放锁，进入 TIMED_WAITING
//                    lock.wait(10000000);

                    //持有锁，进入 TIMED_WAITING
                    Thread.sleep(1000000);//time_wating
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t4");
        t4.start();

        final Thread t5 = new Thread(() -> {
            try {
                t2.join();//wating
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t5");
        t5.start();

        final Thread t6 = new Thread(() -> {
            //等待锁，进入BLOCKED
            synchronized (lock){//blocked
                try {
                    log.debug("获取类锁。。。");
                    Thread.sleep(1000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "6");
        t6.start();

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.debug("t1 state {}",t1.getState());
        log.debug("t2 state {}",t2.getState());
        log.debug("t3 state {}",t3.getState());
        log.debug("t4 state {}",t4.getState());
        log.debug("t5 state {}",t5.getState());
        log.debug("t6 state {}",t6.getState());
    }
}
